﻿using System;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
#endif


using UnityEngine.UI;
using Screen = UnityEngine.Device.Screen;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    [DefaultExecutionOrder(30000)][DisallowMultipleComponent]
    public class ScreenOrientDetector:MonoBehaviour
    {

        public int GetListenerCount()
        {
            return ScreenOrientManager.GetInstance().GetListenerCount();
        }

        void Awake()
        {
            ScreenOrientManager.GetInstance().Initialize();
            
        }

        private void OnEnable()
        {
#if UNITY_EDITOR
            ScreenOrientManager.GetInstance().Refresh();
#endif
        }
        
        /// <summary>
        /// 用来监听屏幕方向变化
        /// </summary>
        void Update()
        {
            ScreenOrientManager.GetInstance().Update();
        }
#if UNITY_EDITOR


        public void Refresh()
        {
         
          
            var screenOrientItems = this.transform.GetComponentsInChildren<ScreenOrientItem>();
            foreach (var item in screenOrientItems)
            {
                item.RefreshCollectUsedListener();  
            }
            ScreenOrientManager.GetInstance().Refresh();     
        }

        public void ForceCleanup()
        {
            ScreenOrientManager.GetInstance().ForceCleanup();
            var screenOrientItems = this.transform.GetComponentsInChildren<ScreenOrientItem>();
            foreach (var item in screenOrientItems)
            {
                item.RefreshCollectUsedListener();
            }
        }
        private void OnValidate()
        {
            if (EditorApplication.isCompiling)
            {
                //UnityEngine.Debug.LogWarning("Unity编辑器---Compiling.......");
                return;
            }
            
            if(EditorApplication.isPlayingOrWillChangePlaymode || ScreenOrientManager.GetInstance().EditorChangePlayMode)
            {
                // UnityEngine.Debug.LogWarning("Unity编辑器---isPlayingOrWillChangePlaymode.......");
                return;
            }
            
            //TODO: 为什么会有不在Stage中的loadingUI
            
            //这个是为了打开prefab,环境的那个canvas要更改
            var screenOrientItems = GameObject.FindObjectsOfType<ScreenOrientItem>();
            bool isInPrefabStage = IsEditingInPrefabStage();
            foreach (var soi in screenOrientItems)
            {
                if (soi.gameObject.scene == SceneManager.GetActiveScene() )
                {
                    if (!isInPrefabStage)
                    {
                        UnityEngine.Debug.Log(soi.name + " is in the scene.");
                        UnityEngine.Debug.Log("【OnValidate】  ScreenOrientationItem   name: "+soi.gameObject.name,soi.gameObject);
                        soi.RefreshCollectUsedListener();
                    }
                }
                else 
                {
                    if (isInPrefabStage)
                    {
                        var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                        if(prefabStage.openedFromInstanceObject == soi.gameObject)
                        {
                            UnityEngine.Debug.Log(soi.name + " is in the Prefab Stage."); 
                            soi.RefreshCollectUsedListener();
                        }
                    }
                }
            }
         //   ScreenOrientManager.GetInstance().OnValidate();
        }
        

        public bool IsEditingInPrefabStage()
        {
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
            return prefabStage != null;
        }
#endif
    }
}